#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: opnsense-openconnect
# REQUIRE: SERVERS
# KEYWORD: shutdown
#

. /etc/rc.subr

name=openconnect

stop_cmd=openconnect_stop
start_cmd=openconnect_start
status_cmd=openconnect_status
rcvar=openconnect_enable

load_rc_config opnsense-openconnect
pidfile=/var/run/${name}.pid
command=/usr/local/sbin/${name}

secret=/usr/local/etc/openconnect.secret

[ -z "$openconnect_enable" ] && openconnect_enable="NO"

openconnect_status()
{
	 if [ -n "$rc_pid" ]; then
	     echo "${name} is running as pid $rc_pid."
	     return 0
	 else
	     echo "${name} is not running."
	 fi
}

openconnect_stop()
{
        if [ -n "$rc_pid" ]; then
            echo "stopping openconnect"
            ifconfig ocvpn0 name tun30000
            kill -2 ${rc_pid}
        else
            echo "${name} is not running."
        fi
}

openconnect_start()
{
        echo "starting openconnect"
	/usr/local/sbin/openconnect ${openconnect_flags} < /usr/local/etc/openconnect.secret 2>&1 > /dev/null
        sleep 5
	ifconfig tun30000 name ocvpn0
	ifconfig ocvpn0 group ocvpn
	return 0
}

run_rc_command $1
